<template>
  <div style="padding: 10px 15px">
    <!-- 查询条件-->
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="请输入单号"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item seachbttn"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
        >查询</el-button
      >
    </div>
    <div class="filter_fenge"></div>
    <div class="filter-container">
      <el-button
        class="filter-item bttnleft"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
        v-if="Jurisdiction('添加')"
        >新增</el-button
      >
      <el-button
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="eleteClick()"
        v-if="Jurisdiction('删除')"
        >删除</el-button
      >
      <el-button class="filter-item" type="primary" @click="batchClick"
        >批量处理</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="exportFormVisible = true"
        >导出</el-button
      >
      <el-checkbox v-model="errorchecked" @change="errorcheckedChange($event)"
        >仅显示差异订单</el-checkbox
      >
    </div>
    <!--查询条件 end-->

    <!--table-->
    <el-table
      v-loading="listLoading"
      :data="DifOrdsData"
      fit
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionChange"
      stripe
    >
      <el-table-column type="selection" width="55" fixed="left" />
      <el-table-column label="处理状态" align="left" min-width="120px">
        <template slot-scope="{ row }">
          <span v-if="row.difType != 6 && row.dealStt == 0" class="green">{{
            row.dealSttStr
          }}</span>
          <span v-if="row.difType != 6 && row.dealStt == 1" class="red">{{
            row.dealSttStr
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对账结果" align="left" min-width="180px">
        <template slot-scope="{ row }">
          <span v-if="row.difType == 6" class="green">{{
            row.difTypeStr
          }}</span>
          <span v-if="row.difType != 6" class="red">{{ row.difTypeStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="业务日期" align="left" min-width="100px">
        <template slot-scope="{ row }">
          <span>{{ row.dtStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="企业" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.cpnIDStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="机构" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.orgIDStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付账户" align="left" min-width="220px">
        <template slot-scope="{ row }">
          <span>{{ row.act }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付-渠道" align="left" min-width="230px">
        <template slot-scope="{ row }">
          <span>{{ row.payStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="西联商户订单号" align="left" min-width="300px">
        <template slot-scope="{ row }">
          <span>{{ row.merchanNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="第三方平台订单号" align="left" min-width="300px">
        <template slot-scope="{ row }">
          <span>{{ row.othNo }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="西联支付平台金额/元"
        align="right"
        min-width="170px"
      >
        <template slot-scope="{ row }">
          <span>{{ row.xlMoneyStr }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="第三方平台金额/元"
        align="right"
        min-width="170px"
      >
        <template slot-scope="{ row }">
          <span>{{ row.thMoneyStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="西联ERP金额/元" align="right" min-width="170px">
        <template slot-scope="{ row }">
          <span>{{ row.erpMoneyStr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="对账类型" align="left" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.billTypeStr }}</span>
        </template>
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.dealStt != 0 && scope.row.difType != 6"
            @click="AuditClick(scope.row)"
            type="primary"
            size="mini"
            >点击处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="listQuery.total > 0"
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="fetchData(this)"
    />

    <!--table end -->

    <!--详情-->
    <el-dialog
      title="订单处理"
      :visible.sync="dialogFormVisible"
      width="30%"
      :show-close="false"
      :before-close="handleClose"
    >
      <el-form ref="form" width="60%">
        <el-form-item label="处理描述">
          <el-input
            type="textarea"
            v-model="Dscription"
            style="width: 400px; hieght: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件上传">
          <el-upload
            class="upload-demo"
            :action="this.baseurl"
            :on-error="FileError"
            :on-success="FileSuccess"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-progress="Uploading"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :data="filedatas"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveForm">确 定</el-button>
      </div>
    </el-dialog>
    <!--弹出层  新增-->
    <el-dialog title="导出" :visible.sync="exportFormVisible" width="20%">
      <el-form :model="downloadform" :inline="true" label-position="right">
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          :loading="downloadform.nowexportLoading"
          @click="DifOrdsExport(false)"
          >按当前页导出</el-button
        >
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-download"
          :loading="downloadform.allexportLoading"
          @click="DifOrdsExport(true)"
          >全部导出</el-button
        >
      </el-form>
    </el-dialog>

    <!--弹出层 end-->
  </div>
</template>


<script>
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import { AjaxToken, TipConfirm } from "@/api/common";
import { SelectAjax } from "@/api/common";
import { Jurisdiction } from "@/api/common";
import sessionStorage from "@/api/Storage";
import Audit from "./components/Audit";
import commconfig from "@/comm.json";
import "url-search-params-polyfill";
export default {
  name: "DifOrds",
  components: { Pagination, Audit },
  methods: {
    errorcheckedChange(e) {
      this.fetchData();
    },
    DifOrdsExport(Export) {
      if (Export) {
        this.downloadform.allexportLoading = true;
      } else {
        this.downloadform.nowexportLoading = true;
      }
      backstage.DifOrdsExport(this, Export);
    },
    Uploading() {
      this.loading = this.$loading({
        lock: true,
        text: "正在上传中...",
        spinner: "el-icon-loading",
        target: document.querySelector(".upload-demo"),
      });
    },
    handleRemove(file, fileList) {
      this.loading.close();
      console.log(file, fileList);
    },
    handlePreview(file) {
      this.loading.close();
      console.log(file);
    },
    FileSuccess(response, file, fileList) {
      this.loading.close();
      if (!response.success) {
        this.$message({
          message: "上传失败," + response.message,
          type: "error",
        });
      } else {
        this.succesfile.push(response.data.data);
      }
    },
    FileError(err, file, fileList) {
      this.loading.close();
    },
    handleExceed(files, fileList) {
      this.loading.close();
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      //return this.$confirm(`确定移除 ${file.name}？`);
    },
    handleClose() {},
    //权限
    Jurisdiction(moduleActionName) {
      return Jurisdiction(moduleActionName, this);
    },
    //多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //多选赋值
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    //搜索
    handleFilter() {
      this.listQuery.page = 1;
      this.fetchData();
    },
    AuditClick(row) {
      this.difordsId = row.id;
      this.filedatas.typ = 0;
      this.filedatas.difordsId = row.id;
      this.dialogFormVisible = true;
    },
    //保存
    saveForm() {
      var arr = this.multipleSelection;
      var params = new URLSearchParams();
      params.append("Dscription", this.Dscription);
      params.append("ObjId", this.difordsId);
      params.append("typ", this.filedatas.typ);
      params.append("ObjIds", JSON.stringify(this.filedatas.difordsIds));
      params.append("CpnId", this.logincpnid);
      backstage.AddRcnHandRcd(this, params);
    },
    //删除
    deleteClick(row) {
      var multis = [];
      var params = new URLSearchParams();
      if (row == undefined || row == "") {
        var arr = this.multipleSelection;
        if (arr == undefined || arr.length <= 0) {
          this.$message({
            showClose: true,
            message: "请至少选中一行",
            type: "error",
          });
          this.listLoading = false;
          return;
        } else {
          for (var i = 0; i < arr.length; i++) {
            multis.push(arr[i].id);
          }
          params.append("delds", JSON.stringify(multis));
        }
      } else {
        multis.push(row.id);
        params.append("delds", JSON.stringify(multis));
      }
      this.$confirm("确认删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "error",
      })
        .then((a) => {
          this.listLoading = true;
          backstage.DelDifOrds(this, params);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.listLoading = false;
        });
    },
    //批量处理异常订单
    batchClick(row) {
      this.filedatas.typ = 1;
      this.Dscription = "";
      this.Dscription += "批量处理单号\n";
      var multis = [];
      var arr = this.multipleSelection;
      if (arr == undefined || arr.length <= 0) {
        this.$message({
          showClose: true,
          message: "请至少选中一行",
          type: "error",
        });
        this.listLoading = false;
        return;
      } else {
        for (var i = 0; i < arr.length; i++) {
          multis.push(arr[i].id);
          this.Dscription += arr[i].merchanNo + "\n";
        }
        this.filedatas.difordsIds = multis;
      }

      this.dialogFormVisible = true;
    },
    //查询
    fetchData() {
      this.listLoading = true;
      var params = new URLSearchParams();
      params.append(
        "Parms",
        '{"page":' +
          this.listQuery.page +
          ',"rows":' +
          this.listQuery.limit +
          "}"
      );
      var user = JSON.parse(sessionStorage.getItem("userinfo"));
      params.append("Title", this.listQuery.title);
      params.append("BtchId", this.btchid);

      params.append("Act", this.act);
      params.append("OrgID", this.orgid);
      params.append("PayTyp", this.paytype);
      params.append(
        "Tp",
        this.$route.query.tp == undefined ? "" : this.$route.query.tp
      );
      params.append("CpnID", user.cpnId == "system" ? "" : user.cpnId);
      params.append("IsError", this.errorchecked);

      backstage.GetDifOrdsLst(this, params);
    },
  },
  //初始化
  created() {
    this.btchid =
      this.$route.query.btchid == undefined ? "" : this.$route.query.btchid;
    this.act = this.$route.query.act == undefined ? "" : this.$route.query.act;
    this.paytype =
      this.$route.query.paytype == undefined ? "" : this.$route.query.paytype;
    this.orgid =
      this.$route.query.orgid == undefined ? "" : this.$route.query.orgid;

    this.baseurl = commconfig.baseapi + "UploadFileAttch";
    this.fetchData();
  },
  activated() {
    this.btchid =
      this.$route.query.btchid == undefined ? "" : this.$route.query.btchid;
    this.act = this.$route.query.act == undefined ? "" : this.$route.query.act;
    this.paytype =
      this.$route.query.paytype == undefined ? "" : this.$route.query.paytype;
    this.orgid =
      this.$route.query.orgid == undefined ? "" : this.$route.query.orgid;

    this.baseurl = commconfig.baseapi + "UploadFileAttch";
    this.fetchData();
  },
  data() {
    return {
      errorchecked: true,
      logincpnid: "",
      Dscription: "",
      fileList: [],
      baseurl: "",
      loading: null,
      succesfile: [],
      difordsId: "",
      filedatas: {
        difordsId: "",
        difordsIds: [],
        typ: 0, //0-单个处理,1-批量处理
      },
      btchid: "",
      act: "",
      paytype: "",
      orgid: "",
      multipleSelection: [],
      formLabelWidth: "120px",

      DifOrdsData: null,
      listQuery: {
        title: "",
        total: 0,
        page: 1,
        limit: 10,
      },
      downloadform: {
        allexportLoading: false,
        nowexportLoading: false,
      },
      listLoading: true,
      dialogFormVisible: false,
      dialogdetailsVisible: false,
      exportFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "修改",
        create: "添加",
      },
      form: {
        id: "",
        dt: "",
        btchId: "",
        merchanNo: "",
        othNo: "",
        cpnID: "",
        orgID: "",
        act: "",
        payChannel: "",
        payTyp: "",
        tHMoney: "",
        xLMoney: "",
        eRPMoney: "",
        difType: "",
        dealStt: "",
        billType: "",
      },
      details_form: {
        id: "",
        dt: "",
        btchId: "",
        merchanNo: "",
        othNo: "",
        cpnID: "",
        orgID: "",
        act: "",
        payChannel: "",
        payTyp: "",
        tHMoney: "",
        xLMoney: "",
        eRPMoney: "",
        difType: "",
        dealStt: "",
        billType: "",
      },
    };
  },
};
//后台事件
var backstage = {
  //获取DifOrds集合
  GetDifOrdsLst: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "GetDifOrdsLst",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data.data;
        thisobj.DifOrdsData = body.rows;
        thisobj.listQuery.page = body.page;
        thisobj.listQuery.total = body.records;
      },
      function (err) {}
    );
  },
  DifOrdsExport: function (thisobj, Export) {
    var tp =
      thisobj.$route.query.tp == undefined ? "" : thisobj.$route.query.tp;
    var url =
      commconfig.baseapi +
      "DifOrdsExport?Parms=" +
      '{"page": "' +
      thisobj.listQuery.page +
      '","rows": "' +
      thisobj.listQuery.limit +
      '"}' +
      "&Export=" +
      Export +
      "&Title=" +
      thisobj.listQuery.title +
      "&BtchId=" +
      thisobj.btchid +
      "&Tp=" +
      tp +
      "&Act=" +
      thisobj.act +
      "&PayTyp=" +
      thisobj.paytype +
      "&OrgID=" +
      thisobj.orgid;
    if (Export) {
      thisobj.downloadform.allexportLoading = false;
    } else {
      thisobj.downloadform.nowexportLoading = false;
    }
    window.location.href = url;
  },
  SaveDifOrds: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "SaveDifOrds",
      params,
      "post",
      function (res) {
        var body = res.data;
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 2000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },
  DelDifOrds: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "DelDifOrds",
      params,
      "post",
      function (res) {
        thisobj.listLoading = false;
        var body = res.data;
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 2000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },
  AddRcnHandRcd: function (thisobj, params) {
    AjaxToken(
      thisobj,
      "AddRcnHandRcd",
      params,
      "post",
      function (res) {
        var body = res.data;
        if (body.success) {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "success",
            duration: 2000,
          });
          thisobj.fetchData();
          thisobj.dialogFormVisible = false;
        } else {
          thisobj.$message({
            showClose: true,
            message: body.message,
            type: "error",
          });
        }
      },
      function (err) {}
    );
  },
};
</script>
